package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

//合同下的货物
@Service
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public void save(ContractProduct contractProduct) {

        //1、设置货物的UUID、 总金额
        contractProduct.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        Double total = contractProduct.getPrice() * contractProduct.getCnumber();
        contractProduct.setAmount(total);
        //2、保存货物
        contractProductDao.insertSelective(contractProduct);
        //3、根据货物的id获取合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //4、修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount() + total);
        //5、修改合同的货物数量
        contract.setProNum(contract.getProNum() + 1);
        //6、修改合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void update(ContractProduct contractProduct) {
        //1、根据货物id 获取以前的货物对象
        ContractProduct old_contractProduct = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        //2、获取以前货物的总金额
        Double old_amount = old_contractProduct.getAmount();
        //3、设置  计算最新的总金额 赋值给货物
        Double new_total = contractProduct.getPrice() * contractProduct.getCnumber();
        //4、修改货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        //5、根据货物合同id查询合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //6、修改合同金额
        contract.setTotalAmount(contractProduct.getAmount() - old_amount + new_total);
        //7、 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override          //ID： 货物id
    public void delete(String id) {
        //1、根据获取id获取货物
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //2、获取货物的总金额
        Double total = contractProduct.getAmount();
        //3、根据货物的id获取附件（list集合）
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        //设置条件   根据货物的id查询附件
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);
        //4、 对附件  进行遍历循环 累加货物+附件的总金额
        if (list.size() > 0) {
            for (ExtCproduct extCproduct : list) {
                total += extCproduct.getAmount();
                //5、 删除附件
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }
        //6、删除货物
        contractProductDao.deleteByPrimaryKey(id);
        //7、获取合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //8、 设置合同的总金额
        contract.setTotalAmount(contract.getTotalAmount() - total);
        //9、 设置合同的货物数和附件数
        contract.setProNum(contract.getProNum() - 1);
        contract.setExtNum(contract.getExtNum()-list.size());
        //10、 修改合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void saveProductExcel(List<ContractProduct> list) {
        //遍历获取每一个货物并保存
        for (ContractProduct contractProduct : list) {
            save(contractProduct);
        }
    }
    //根据船期查询并封装为vo对象
    public List<ContractProductVo> findVoByShipTime(String inputDate, String companyId){
        return contractProductDao.findByShipTime(inputDate,companyId);
    }
}
